package mall

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"main.go/global"
	"main.go/model/common"
	"main.go/model/common/request"
	"main.go/model/common/response"
	"main.go/model/mall"
	mallReq "main.go/model/mall/request"
	"main.go/utils"
)

type UserApi struct{}

func (a *UserApi) Login(c *gin.Context) {
	var l mallReq.MallUserReq
	err := c.ShouldBindJSON(&l)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(l, utils.MobileVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(l, utils.PasswordVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	user, err := userService.Login(&l)
	if err != nil {
		global.EVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Error(err))
		response.FailWithMessage("用户名不存在或者密码错误", c)
		return
	}
	if *user.Enable {
		global.EVA_LOG.Error("登陆失败! 用户被禁止登录!")
		response.FailWithMessage("用户被禁止登录", c)
		return
	}
	token := utils.GenerateUUID()
	err = utils.SetToken(common.TokenInfo{
		UUID:   user.UUID,
		Token:  token,
		UserID: uint64(user.ID),
		Mobile: user.Mobile,
		Source: "app",
	})
	if err != nil {
		global.EVA_LOG.Error("登录失败", zap.Error(err))
		response.FailWithMessage("登录失败", c)
		return
	}

	response.OkWithDetailed(token, "登录成功", c)

}
func (a *UserApi) ChangePassword(c *gin.Context) {
	var req mallReq.ChangePasswordReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.PasswordVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.NewPasswordVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	uid := utils.GetUserID(c)
	user := &mall.MallUser{EVA_MODEL: global.EVA_MODEL{ID: uid}, Password: req.Password}
	_, err = userService.ChangePassword(user, req.NewPassword)

	if err != nil {
		global.EVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败，原密码与当前账户不符", c)
		return
	}
	response.OkWithMessage("修改成功", c)
}

func (a *UserApi) ResetPassword(c *gin.Context) {
	var reqId request.GetById
	err := c.ShouldBindJSON(&reqId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(reqId, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = userService.ResetPassword(uint(reqId.ID))

	if err != nil {
		global.EVA_LOG.Error("重置失败!", zap.Error(err))
		response.FailWithMessage("重置", c)
		return
	}
	response.OkWithMessage("重置成功", c)
}

func (a *UserApi) Create(c *gin.Context) {
	var r mall.MallUser
	err := c.ShouldBindJSON(&r)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(r, utils.MobileVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	r.Password = "123456"
	if len(r.NickName) == 0 {
		r.NickName = fmt.Sprintf("管理员_%v", r.Mobile)
	}
	userReturn, err := userService.Create(&r)
	if err != nil {
		global.EVA_LOG.Error("注册失败!", zap.Error(err))
		response.FailWithDetailed(userReturn, "注册失败", c)
		return
	}
	response.OkWithDetailed(userReturn, "注册失败", c)
}
func (a *UserApi) Delete(c *gin.Context) {
	var reqId request.GetById
	err := c.ShouldBindJSON(&reqId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(reqId, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = userService.Delete(reqId.ID)
	if err != nil {
		global.EVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)

}
func (a *UserApi) Update(c *gin.Context) {
	var data mall.MallUser
	err := c.ShouldBindJSON(&data)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(data, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	_, err = userService.Update(data)
	if err != nil {
		global.EVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}
func (a *UserApi) List(c *gin.Context) {
	var pageInfo mallReq.MallUserListReq
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := userService.List(&pageInfo)
	if err != nil {
		global.EVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

func (a *UserApi) UserInfo(c *gin.Context) {
	userId := utils.GetUserID(c)
	user, err := userService.UserInfo(userId)
	if err != nil {
		global.EVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(user, "获取成功", c)
}
